﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;


namespace Synchros
{
    public partial class FrmConver : Form
    {
        public FrmConver()
        {
            InitializeComponent();
        }
       


        private void FrmConver_Load(object sender, EventArgs e)
        {

        }
        List<string> lstSyncname = new List<string>();

        private void lblConvert_Click(object sender, EventArgs e)
        {
            string strGroupOld=cbxGroupOld.Text.Trim();
            string strServerOld=cbxServerOld.Text.Trim();
            string strDatabaseOld=cbxDatabaseOld.Text.Trim();

            string strGroupNew=cbxGroupNew.Text.Trim();
            string strServerNew=cbxServerNew.Text.Trim();
            string strDatabaseNew=cbxDatabaseNew.Text.Trim();

            string strOldGsd = SqlHelper.GetGsd(strGroupOld, strServerOld, strDatabaseOld);
            string strNewGsd = SqlHelper.GetGsd(strGroupNew, strServerNew, strDatabaseNew);

            if (strOldGsd == strNewGsd) return;

            int iCountFile = 0;
            int iCountConf = 0;
            try
            {
                System.IO.DirectoryInfo di = new DirectoryInfo(SqlHelper.strSyncLineDirPath);
                FileInfo[] fis = di.GetFiles();
                XmlDocument doc = new XmlDocument();
                for (int iFile = 0; iFile < fis.Length; iFile++)
                {
                    string strFileFullName = fis[iFile].FullName;
                    if (strFileFullName.Contains(strGroupOld))//strFileFullName.Contains(strGroupNew)
                    {
                        doc.Load(strFileFullName);
                        XmlNodeList nodelist = doc.DocumentElement.ChildNodes;
                        for (int iNode = 0; iNode < nodelist.Count; iNode++)
                        {
                            string strOldSyncname=nodelist[iNode]["SyncName"].InnerText;
                            if(strOldSyncname.Contains(strOldGsd))
                            {
                                //更新配置内容
                                nodelist[iNode]["SyncName"].InnerText = strOldSyncname.Replace(strOldGsd, strNewGsd);
                                iCountConf++;
                            }
                        }

                        if (strFileFullName.Contains(strOldGsd))
                        {
                            //更新后的FullName
                            string strNewFullName = strFileFullName.Replace(strOldGsd, strNewGsd);

                            //保存到新文件
                            doc.Save(strNewFullName);

                            if (strNewFullName != strFileFullName)
                            {
                                //删除旧文件
                                fis[iFile].Delete();
                            }
                            iCountFile++;
                        }
                    }
                }
                MessageBox.Show(string.Format("转换文件:{0}个\n转换配置:{1}条", iCountFile, iCountConf));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
